package com.onemore.compare;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpResponse;
import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onemore.task.OneConfig;
import com.onemore.task.model.ModelGeneral;
import com.onemore.task.model.ModelOneErp2Wms;
import com.onemore.task.model.ModelSpjhd;

public class CompareErp2Wms  extends CompareBase{
	public static void process(HttpRequest req, HttpResponse resp) throws SQLException, InterruptedException
	{	
		String filterDate = "";
		String cangku = "'09001','09002'";
		String ycdfilter = "'1018','2001'";
		String starDate = req.getParameter("starDate");
		String endDate = req.getParameter("endDate");
		Properties properties= OneConfig.getProperties();
		if(properties != null) {
			String str = OneConfig.getProperties().getProperty("cangku");
			if(str != null) {
				cangku = str;
			}

			if(starDate == null) {
				str = OneConfig.getProperties().getProperty("starDate");
				if(str != null) {
					starDate = str;
				}
			}

			str = OneConfig.getProperties().getProperty("ycdfilter");
			if(str != null) {
				ycdfilter = str;
			}
		}
		if(starDate != null && starDate.length() > 0) {
			filterDate = " RQ>='"+starDate+"' AND";
		}else {
			starDate = "2018";
		}
		if(endDate != null && endDate.length() > 0) {
			filterDate += " RQ<='"+endDate+"' AND";
		}
		List<Map<String, Object>> listSpjhd = new ArrayList<Map<String, Object>>();
		String sql = req.getParameter("sql");
		/*if(sql == null)
			sql = "SELECT MAX(wms.BillId) AS BillId, DJBH, CONVERT(VARCHAR(11),MAX(RQ),120) AS CDate,sum(QTYIN) AS QTYIN, SUM (SL) AS SL FROM (select DJBH,RQ,SL from JSEND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from JTSND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from DSEND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from QDTHD where RQ>='"+starDate+"' and YS='1' AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from PSEND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from SDTHD where RQ>='"+starDate+"' and YS='1' AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from FSEND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from FTSND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM2 IN ("+cangku+") UNION ALL select DJBH,RQ,SL from YSEND where RQ>='"+starDate+"' and JZ='1' and ((sp='1' and JS='1') or (sp='0' and JS='0')) AND DM1 IN ("+cangku+") and NOT (dm1 IN ("+ycdfilter+") AND dm2 IN ("+ycdfilter+")) UNION ALL select DJBH,RQ,SL from SPYCD where RQ>='"+starDate+"' and YS='1' AND ISNULL(BYZD3,0)<>'0' AND DM2 IN ("+cangku+") AND YDJH not like 'PF%' AND NOT (dm1 IN ("+ycdfilter+") AND dm2 IN ("+ycdfilter+"))) erp LEFT JOIN ONE_WMS_TZD wms ON erp.DJBH = wms.syncbillid WHERE "+filterDate+" ( wms.BillId IS NULL OR wms.syncbillid IS NULL ) GROUP BY DJBH";
		listSpjhd= ModelGeneral.getCompareList(sql);*/
		listSpjhd = ModelOneErp2Wms.getList(starDate,endDate);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		jsonArray.addAll(listSpjhd);
		jsonObject.put("data", jsonArray);
		String str = jsonObject.toJSONString();
		resp.write(str);
	}
	public static String getString() throws SQLException, InterruptedException
	{	
		List<Map<String, Object>> listSpjhd = ModelSpjhd.getCompare();
		return listSpjhd.toString();
	}
}
